package com.xuelangyun.form.config;

import com.xuelangyun.form.common.redis.RedisOperateUtils;
import com.xuelangyun.form.common.utils.Constant;
import com.xuelangyun.form.common.utils.MD5Utils;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xuelangyun.form.sys.entity.SysUserEntity;

/**
 * SysConfig
 *
 * @author weiqing.huang
 * @version 1.0
 * @date 2024年6月7日17:07:44
 */
@Component
public class SysConfig {

    Logger logger = LoggerFactory.getLogger(getClass());
    String salt = "39efqRGvSswrDhTZTWR2";
    @Autowired
    private RedisOperateUtils redisUtils;

    public SysConfig() {

    }

    public SysUserEntity getAdminUser() {
        SysUserEntity sae = new SysUserEntity();
        sae.setUserId(Constant.SUPER_ADMIN);
        sae.setUserNo(Constant.SUPER_ADMIN);
        //String pass = "123456";
        String adminPass = "";

        // 从nginx中获取密码
        String passKey = getClass().getSimpleName() + "." + Constant.SUPER_ADMIN + ".pass";
        if (redisUtils.containKey(passKey)) {
            adminPass = redisUtils.get(passKey, String.class);
        } else {
            String pass = "admin";
            adminPass = MD5Utils.md5Pass(pass, salt);

            redisUtils.set(passKey, adminPass, 60 * 60 * 24 * 3650);
            logger.info("set pass , key: {} ", passKey);
        }
        sae.setPassword(adminPass);
        sae.setSalt(salt);
        sae.setStatus(1);
        sae.setUsername(Constant.SUPER_ADMIN);
        return sae;
    }

    public void updatePassword(String password) {
        // 修改redis内的超管密码
        String passKey = getClass().getSimpleName() + "." + Constant.SUPER_ADMIN + ".pass";
        redisUtils.set(passKey, password, 60 * 60 * 24 * 3650);
    }
}
